﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Zyg.SSO.Web.User
{
    public partial class Security : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Zyg.SSO.Core.Business.Account account = (Zyg.SSO.Core.Business.Account)Session["User"];

                OrigianlPwdQuestion.Text = account.PassQuestion;
            }
        }

        protected void BtnPwd_Click(object sender, EventArgs e)
        {
            if (NewPwd.Text.Length > 5 && NewPwd.Text.Length < 17)
            {
                Zyg.SSO.Core.Business.Account account = (Zyg.SSO.Core.Business.Account)Session["User"];

                Zyg.Security.SecurityHelper sh = new Zyg.Security.SecurityHelper();

                if (account.Password == sh.ComputeMD5Hash(OriginalPwd.Text))
                {
                    account.Password = sh.ComputeMD5Hash(OriginalPwd.Text);
                    account.Save();

                    Session["User"] = account;
                }
                else
                {
                    WriteBackScript("alert('原密码不正确')");
                }
            }
            else
            {
                WriteBackScript("alert('新密码长度必须在6到16之间');");
            }
        }

        protected void BtnQuestion_Click(object sender, EventArgs e)
        {
            if (NewPwdQuestion.Text.Trim().Length > 0 && NewPwdAnswer.Text.Trim().Length > 0)
            {
                Zyg.SSO.Core.Business.Account account = (Zyg.SSO.Core.Business.Account)Session["User"];
                Zyg.Security.SecurityHelper sh = new Zyg.Security.SecurityHelper();

                account.PassQuestion = NewPwdQuestion.Text.Trim();
                account.PassAnswer = sh.ComputeMD5Hash(NewPwdAnswer.Text.Trim());

                account.Save();

                Session["User"] = account;
            }
            else
            {
                WriteBackScript("alert('输入不能为空')");
            }
        }
    }
}
